In-class Exercise 3

In this exercise, we will be exploring Interactive Data Visualisation with R by using the packages ggiraph, plotyr and gganimate

Mak Han Ren https://www.linkedin.com/in/mak-han-ren/ (School of Computing and Information Systems, SMU)https://scis.smu.edu.sg
2022-02-03

1.0 Installing and Launching R Packages

Before you get started, you are required:

Next, you will use the code chunk below to install and launch ggiraph, DT, plotly, tidyverse, patchwork, readxl, gifski and gapminder in RStudio.

packages = c('ggiraph', 'plotly', 
             'DT', 'patchwork',
             'gganimate', 'tidyverse',
             'readxl', 'gifski', 'gapminder')
for(p in packages){library
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p, character.only = T)
}

2.0 Importing Data

exam_data <- read_csv("data/Exam_data.csv")

3.0 Interactive Data Visualisation - ggiraph methods

Tooltip effect with tooltip aesthetic

p <- ggplot(data=exam_data, 
       aes(x = MATHS)) +
  geom_dotplot_interactive(
    aes(tooltip = ID),
    stackgroups = TRUE, 
    binwidth = 1, 
    method = "histodot") +
  scale_y_continuous(NULL, 
                     breaks = NULL)
girafe(
  ggobj = p,
  width_svg = 6,
  height_svg = 6*0.618
)

Tooltip effect with data_id aesthetic

Interactivity: Elements associated with a data_id (i.eCLASS) will be highlighted upon mouse over.

Note that the default value of the hover css is hover_css = “fill:orange;”.

p <- ggplot(data=exam_data, 
       aes(x = MATHS)) +
  geom_dotplot_interactive(           
    aes(data_id = c(CLASS)),
    stackgroups = TRUE,               
    binwidth = 1,                        
    method = "histodot") +               
  scale_y_continuous(NULL,               
                     breaks = NULL)
girafe(                                  
  ggobj = p,                             
  width_svg = 6,                         
  height_svg = 6*0.618                      
)

Styling hover effect

In the code chunk below, css codes are used tochange the highlighting effect.

p <- ggplot(data=exam_data, 
       aes(x = MATHS)) +
  geom_dotplot_interactive(              
    aes(data_id = CLASS),              
    stackgroups = TRUE,                  
    binwidth = 1,                        
    method = "histodot") +               
  scale_y_continuous(NULL,               
                     breaks = NULL)
girafe(                                  
  ggobj = p,                             
  width_svg = 6,                         
  height_svg = 6*0.618,
  options = list(
    opts_hover(css = "fill: #202020;"),
    opts_hover_inv(css = "opacity:0.2;")
  )
)

Interactivity: Elements associated with a data_id (i.e CLASS) will be highlighted upon mouse over.

Click effect with onclick

Interactivity: Web document link with a data object will be displayed on the web browser upon mouse click.

exam_data$onclick <- sprintf("window.open(\"%s%s\")",
"https://www.moe.gov.sg/schoolfinder?journey=Primary%20school", as.character(exam_data$ID) )
p <- ggplot(data=exam_data, 
       aes(x = MATHS)) +
  geom_dotplot_interactive(              
    aes(onclick = onclick),
    stackgroups = TRUE,                  
    binwidth = 1,                        
    method = "histodot") +               
  scale_y_continuous(NULL,               
                     breaks = NULL)
girafe(                                  
  ggobj = p,                             
  width_svg = 6,                         
  height_svg = 6*0.618)

Coordinated Multiple Views with ggiraph

Coordinated multiple views methods has beenimplemented in the data visualisation on the right. + when a data point of one of the dotplot isselected, the corresponding data point ID on thesecond data visualisation will be highlighted too.

In order to build a coordinated multiple views, the following programming strategy will be used:

p1 <- ggplot(data=exam_data, 
       aes(x = MATHS)) +
  geom_dotplot_interactive(              
    aes(data_id = ID),              
    stackgroups = TRUE,                  
    binwidth = 1,                        
    method = "histodot") +  
  coord_cartesian(xlim=c(0,100)) +
  scale_y_continuous(NULL,               
                     breaks = NULL)
p2 <- ggplot(data=exam_data, 
       aes(x = ENGLISH)) +
  geom_dotplot_interactive(              
    aes(data_id = ID),              
    stackgroups = TRUE,                  
    binwidth = 1,                        
    method = "histodot") + 
  coord_cartesian(xlim=c(0,100)) +
  scale_y_continuous(NULL,               
                     breaks = NULL)
girafe(code = print(p1 / p2),
       width_svg = 6,
       height_svg = 6,
       options = list(
         opts_hover(css = "fill: #202020;"),
         opts_hover_inv(css = "opacity:0.2;")
         )
       )

The data_id aesthetic is critical to link observations between plots and the tooltip aesthetic is optional but nice to have when mouse over a point.

4.0 - Interactive Data Visualisation - plotly methods

Plotly’s R graphing library create interactive web graphics from ggplot2 graphs and/or a custom interface tothe (MIT-licensed) JavaScript library plotly.js inspired by the grammar of graphics.

Different from other plotly platform, plot.R is free and open source.

There are two ways to create interactive graph by using plotly, they are: + by using plot_ly(), and + by using ggplotly()

Creating an interactive scatter plot: plot_ly() method

The code chunk below plots an interactive scatter plot by using plot_ly().

plot_ly(data = exam_data, 
             x = ~MATHS, 
             y = ~ENGLISH)

Working with visual variable: plot_ly() method

In the code chunk below, color argument is mapped to a qualitative visual variable (i.e. RACE).

plot_ly(data = exam_data, 
        x = ~ENGLISH, 
        y = ~MATHS, 
        color = ~RACE)

To interact with the graph, click on the colour symbol at the legend.

Changing colour pallete: plot_ly() method

In the code chunk below, colors argument is used to change the default colour palette to ColorBrewel colour palette.

plot_ly(data = exam_data, 
        x = ~ENGLISH, 
        y = ~MATHS, 
        color = ~RACE, 
        colors = "Set1")

To interact with the graph, click on the colour symbol at the legend.

Customising colour scheme: plot_ly() method

In the code chunk below, a customised colour scheme is created. Then, colors argument is used to change the default colour palette to the customised colour scheme.

pal <- c("red", "purple", "blue", "green")
plot_ly(data = exam_data, 
        x = ~ENGLISH, 
        y = ~MATHS, 
        color = ~RACE, 
        colors = pal)

Customising tooltip: plot_ly() method

In the code chunk below, text argument is used to change the default tooltip.

plot_ly(data = exam_data, 
        x = ~ENGLISH, 
        y = ~MATHS,
        text = ~paste("Student ID:", ID,
                      "<br>Class:", CLASS),
        color = ~RACE, 
        colors = "Set1")

Working with layout: plot_ly() method

In the code chunk below, layout argument is used to change the default tooltip.

plot_ly(data = exam_data, 
        x = ~ENGLISH, 
        y = ~MATHS,
        text = ~paste("Student ID:", ID,     
                      "<br>Class:", CLASS),  
        color = ~RACE, 
        colors = "Set1") %>%
  layout(title = 'English Score versus Maths Score ',
         xaxis = list(range = c(0, 100)),
         yaxis = list(range = c(0, 100)))

Creating an interactive scatter plot: ggplotly() method

The code chunk below plots an interactive scatter plot by using ggplotly().

p <- ggplot(data=exam_data, 
            aes(x = MATHS,
                y = ENGLISH)) +
  geom_point(dotsize = 1) +
  coord_cartesian(xlim=c(0,100),
                  ylim=c(0,100))
ggplotly(p)

Notice that the only extra line you need to include in the code chunk is ggplotly().

Coordinated Multiple Views with plotly

Code chunk below plots two scatterplots and places them next to each other side-by-side by using subplot() of plotly package.

p1 <- ggplot(data=exam_data, 
              aes(x = MATHS,
                  y = ENGLISH)) +
  geom_point(size=1) +
  coord_cartesian(xlim=c(0,100),
                  ylim=c(0,100))
p2 <- ggplot(data=exam_data, 
            aes(x = MATHS,
                y = SCIENCE)) +
  geom_point(size=1) +
  coord_cartesian(xlim=c(0,100),
                  ylim=c(0,100))
subplot(ggplotly(p1),
        ggplotly(p2))

Coordinated Multiple Views with plotly + Highlights

To create a coordinated scatterplots, highlight_key() of plotly package is used.

d <- highlight_key(exam_data)
p1 <- ggplot(data=d, 
            aes(x = MATHS,
                y = ENGLISH)) +
  geom_point(size=1) +
  coord_cartesian(xlim=c(0,100),
                  ylim=c(0,100))
p2 <- ggplot(data=d, 
            aes(x = MATHS,
                y = SCIENCE)) +
  geom_point(size=1) +
  coord_cartesian(xlim=c(0,100),
                  ylim=c(0,100))
subplot(ggplotly(p1),
        ggplotly(p2))

Click on a data point of one of the scatterplot and see how the corresponding point on the other scatterplot is selected.

Things to learn from the code chunk:

5.0 Interactive Data Table: DT package

A wrapper of the JavaScript Library DataTables

Data objects in R can be rendered as HTML tables using the JavaScript library ‘DataTables’ (typically via R Markdown or Shiny).

DT::datatable(exam_data)

Linked brushing: crosstalk method

d <- highlight_key(exam_data)
p <- ggplot(d, 
            aes(ENGLISH, 
                MATHS)) + 
  geom_point(size=1) +
  coord_cartesian(xlim=c(0,100),
                  ylim=c(0,100))
gg <- highlight(ggplotly(p),
                "plotly_selected")
crosstalk::bscols(gg,
                  DT::datatable(d),
                  widths = 5)

Things to learn from the code chunk:

6.0 Animated Data Visualisation: gganimate methods

gganimate extends the grammar of graphics as implemented by ggplot2 to include the description of animation. It does this by providing a range of new grammar classes that can be added to the plot object in order to customise how it should change with time.

We will be using the following packages in the packages list:

We then import the Data worksheet from GlobalPopulation Excel workbook.

globalPop <- read_xls("data/globalpopulation.xls")

Building a static population bubble plot

We will first plot a static bubble plot

ggplot(globalPop, aes(x = Old, y = Young, 
                      size = Population, 
                      colour = Country)) +
  geom_point(alpha = 0.7, 
             show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  labs(title = 'Year: {frame_time}', 
       x = '% Aged', 
       y = '% Young')

Building an animated bubble chart

Next, we plot an animated population bubble plot to see the difference

ggplot(globalPop, aes(x = Old, y = Young, 
                      size = Population, 
                      colour = Country)) +
  #we use geom_point to control opacity
  geom_point(alpha = 0.7, 
             show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  #we use scale_size to control the sizes of the circles
  scale_size(range = c(2, 12)) +
  labs(title = 'Year: {frame_time}', 
       x = '% Aged', 
       y = '% Young') +
  transition_time(Year) +
  ease_aes('linear')